<template>
  <el-dialog
    :title="!dataForm.businessId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="businessName">
      <el-input v-model="dataForm.businessName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="businessInfo">
      <el-input v-model="dataForm.businessInfo" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createdAt">
      <el-input v-model="dataForm.createdAt" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updatedAt">
      <el-input v-model="dataForm.updatedAt" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="isDeleted">
      <el-input v-model="dataForm.isDeleted" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          businessId: 0,
          businessName: '',
          businessInfo: '',
          createdAt: '',
          updatedAt: '',
          isDeleted: ''
        },
        dataRule: {
          businessName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          businessInfo: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createdAt: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updatedAt: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.businessId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.businessId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/business/info/${this.dataForm.businessId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.businessName = data.business.businessName
                this.dataForm.businessInfo = data.business.businessInfo
                this.dataForm.createdAt = data.business.createdAt
                this.dataForm.updatedAt = data.business.updatedAt
                this.dataForm.isDeleted = data.business.isDeleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/business/${!this.dataForm.businessId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'businessId': this.dataForm.businessId || undefined,
                'businessName': this.dataForm.businessName,
                'businessInfo': this.dataForm.businessInfo,
                'createdAt': this.dataForm.createdAt,
                'updatedAt': this.dataForm.updatedAt,
                'isDeleted': this.dataForm.isDeleted
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
